public with sharing class DataAccessControlUtil {
    
    public Map<String, Schema.SObjectField> getFieldMap(SObject obj)
    {
        return obj.getSObjectType().getDescribe().fields.getMap();
    }


    public Set<String> getAccessibleFields(SObject obj, Map<String, Schema.SObjectField> objectFieldMap, List<String> selectedFieldsList)
    {
        Set<String> allowedROList = new Set<String>();
        for(String s: selectedFieldsList)
        {
            if(objectFieldMap.get(s).getDescribe().isAccessible())
                allowedROList.add(s);
        }
        
        if(allowedROList.isEmpty())
            pageMessage(obj, 'access fields of ');
        return allowedROList;
    }
    
    public Set<String> getFilteredUpdateableFields(Map<String,Schema.SObjectField> fieldsMap, Set<String> toBeFilteredList) 
    {
        Set<String> dupList = toBeFilteredList.clone();
        for(String key: dupList) 
        {
            if(!fieldsMap.get(key).getDescribe().isUpdateable()) 
                toBeFilteredList.remove(key);
        }
        return toBeFilteredList;
    }
    
    public Set<String> getFilteredCreateableFields(Map<String,Schema.SObjectField> fieldsMap, Set<String> toBeFilteredList) 
    {
        Set<String> dupList = toBeFilteredList.clone();
        for(String key: dupList) 
        {
            if(!fieldsMap.get(key).getDescribe().isCreateable()) 
                toBeFilteredList.remove(key);
        }
        return toBeFilteredList;
    }
    
    public String isCreateableObjectByUser(SObject obj)
    {
        if(!obj.getSObjectType().getDescribe().isCreateable())
        {
            return pageMessage(obj, 'create');
        }
        return 'success';
    }
    
    public String isUpdateableObjectByUser(SObject obj)
    {
        if(!obj.getSObjectType().getDescribe().isUpdateable())
        {
            return pageMessage(obj, 'update');
        }
        return 'success';
    }

    public String isDeleteableObjectByUser(SObject obj)
    {
        if(!obj.getSObjectType().getDescribe().isDeletable())
        {
            return pageMessage(obj, 'delete');
        }
        return 'success';
    }

    public String pageMessage(SObject obj, String operation)
    {
        Apexpages.addMessage(new Apexpages.Message(ApexPages.severity.info, 'User cannot '+operation+' '+obj.getSObjectType().getDescribe().getName()+'.'));
        return 'User cannot '+operation+' '+obj.getSObjectType().getDescribe().getName()+'.';
    }
/*  public String insertSObject(SObject obj, Map<String,Schema.SObjectField> fieldsMap, Set<String> filteredCreateableList)
    {
        Set<String> createableFields = fieldsToStringSet(getCreateableFields(fieldsMap));
        for (String f : filteredCreateableList) 
        {
            if (!createableFields.contains(f)) 
                obj.put(f,null);
        }
        
        insert obj;     
        return 'success';
    }*/

/*
    public Map<String, Schema.SObjectField> getCreateableFields(Map<String,Schema.SObjectField> fieldsMap) 
    {
        Map<String, Schema.SObjectField> fields = new Map<String, Schema.SObjectField>();
        for(String key:fieldsMap.keySet()) {
            fields.put(key, fieldsMap.get(key));
        }
        
        for(String key:fieldsMap.keySet()) {
            if(!fieldsMap.get(key).getDescribe().isCreateable()) 
                fields.remove(key);
        }
        return fields;
    }

    public Map<String, Schema.SObjectField> getUpdateableFields(Map<String,Schema.SObjectField> fieldsMap) 
    {
        Map<String, Schema.SObjectField> fields = new Map<String, Schema.SObjectField>();
        for(String key:fieldsMap.keySet()) {
            fields.put(key, fieldsMap.get(key));
        }
        
        for(String key:fieldsMap.keySet()) {
            if(!fieldsMap.get(key).getDescribe().isUpdateable()) 
                fields.remove(key);
        }
        return fields;
    }
    
    
    public List<SObject> getInsertSObjectsList(List<SObject> sObjectList, Map<String,Schema.SObjectField> fieldsMap, Map<String, Schema.SObjectField> createableFields) 
    {
        for(SObject sObj: sObjectList)
        {
            for(String f :fieldsMap.keySet()) 
            {
                if(!createableFields.containsKey(f) && fieldsMap.get(f).getDescribe().isUpdateable())
                {
                    sObj.put(f,null);
                }
            }
        }
        return sObjectList;     
    }
    
    public List<SObject> getUpdateSObjectsList(List<SObject> updatedSObjectList, List<Sobject> existingSObjectList, Map<String,Schema.SObjectField> fieldsMap, Map<String, Schema.SObjectField> updateableFields) 
    {
        SObject tempSobj;
        for(Integer i=0; i<updatedSObjectList.size(); i++)
        {
            tempSObj = updatedSObjectList[i];
            for(String f : fieldsMap.keySet()) 
            {
                if(!updateableFields.containsKey(f) && tempSobj.get(f) != existingSObjectList[i].get(f)) 
                {
                    tempSObj.put(f, existingSObjectList[i].get(f));
                }
            }
        }
        return updatedSObjectList;     
    }
    */

    static testMethod void dacuTestCase2() 
    {
        DataAccessControlUtil dacu = new DataAccessControlUtil();
        SObject sObj = new BatchDetail__c();
        Map<String, Schema.Sobjectfield> fieldsMap = dacu.getFieldMap(sObj);
        String[] expectedFieldMap = new String[]{'Id','Name'};
        Set<String> allowedFields = dacu.getAccessibleFields(sObj, fieldsMap, expectedFieldMap);
        Set<String> filteredList = dacu.getFilteredCreateableFields(fieldsMap, allowedFields);
        filteredList = dacu.getFilteredUpdateableFields(fieldsMap, allowedFields);
        system.assertEquals('success', dacu.isCreateableObjectByUser(sObj));
        system.assertEquals('success', dacu.isUpdateableObjectByUser(sObj));
        system.assertEquals('success', dacu.isDeleteableObjectByUser(sObj));
        dacu.pageMessage(sobj, 'create');
    }

      
}